﻿namespace AutoRevit.Utils;

/// <summary>
/// Autodesk.Revit.DB.XYZ工具
/// </summary>
public class XYZUtils
{
    /// <summary>
    /// 根据坐标点集合转换成直线集合
    /// </summary>
    /// <param name="pointEnumerable"></param>
    /// <param name="transform"></param>
    /// <returns></returns>
    public static IEnumerable<Line> PointsToLines(IEnumerable<XYZ> pointEnumerable, Transform? transform = null)
    {
        XYZ[] points = pointEnumerable.ToArray();

        for (int i = 0; i < points.Length - 1; i++)
        {
            if (points[i].IsAlmostEqualTo(points[i + 1], 1e-6))
            {
                continue;
            }

            var resultLine = Line.CreateBound(points[i], points[i + 1]);

            if (transform is null)
            {
                yield return resultLine;
            }
            else
            {
                yield return (resultLine.CreateTransformed(transform) as Line)!;
            }
        }
    }
}